import 'package:flutter/material.dart';

class Items extends StatefulWidget {
  final index;
  Items({Key key, this.index}) : super(key: key);

  _ItemsState createState() => _ItemsState();
}

class _ItemsState extends State<Items> {
  @override
  void initState() {
    super.initState();
    print('object == init ${widget.index}');
  }

  bool dataNotification(ScrollNotification notification) {
    // notification is ScrollEndNotification 监听滚动结束
    // notification is ScrollUpdateNotification 监听滚动更新

    if (notification is ScrollEndNotification && notification.depth == 0) {
      print('ScrollUpdateNotification');

      //下滑到最底部
      if (notification.metrics.extentAfter == 0.0) {
        print('滑动到了底部');
      }
      //滑动到最顶部
      if (notification.metrics.extentBefore == 0.0) {
        print('滑动到了顶部');
      }
    }
    return true;
  }

  @override
  Widget build(BuildContext context) {
    return NotificationListener(
      onNotification: dataNotification,
      child: ListView.builder(
        physics: NeverScrollableScrollPhysics(),
        padding: EdgeInsets.all(0),
        itemCount: 5,
        itemBuilder: (context, i) => Image.asset(
              widget.index % 2 == 0
                  ? "images/bg.jpg"
                  : widget.index % 3 == 0 ? "images/bg1.jpg" : "images/bg2.jpg",
              fit: BoxFit.cover,
            ),
      ),
    );
  }
}
